package com.econ.powercloud.ems.controller.test;

import com.econ.powercloud.ems.common.util.DateTimeUtil;
import com.econ.powercloud.ems.controller.ApiBaseController;
import com.econ.powercloud.ems.dal.entity.EmsEnergyStation;
import com.econ.powercloud.ems.service.dao.EmsEnergyStationDAO;
import com.econ.powercloud.ems.timing.report.OnOffGridEnergyTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author : zjf
 * @Description : 这个主要是用来修复上网与下网电量的数据
 * @Date : 2023/12/21 9:07
 * @Modified by : zjf
 */
@Api(tags = "修复上网与下网电量数据")
@RestController
@RequestMapping(value = "/api/v2/fix/onOffGrid")
@Slf4j
public class FixedOnOffGridReportController extends ApiBaseController {

    @Autowired
    private OnOffGridEnergyTask offGridEnergyTask;
    @Autowired
    private EmsEnergyStationDAO emsEnergyStationDAO;

    @ApiOperation(value = "更新某个月的生产报表数据")
    @GetMapping(value = "updateProductDataWithMonth")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间", dataType = "long")
    })
    public String updateProductDataWithMonth(@RequestParam("time")Long time){
        //获取指定时间的当月的开始时间
        Long startMonthTime = DateTimeUtil.getStartOfMonth(new Date(time));
        EmsEnergyStation emsEnergyStation = emsEnergyStationDAO.list().get(0);
        //这里主要是为了防止在投运日期前,没有对应的clickhouse的表
        if(emsEnergyStation.getCommissioningDate() > startMonthTime){
            startMonthTime = DateTimeUtil.getStartOfDay(new Date(emsEnergyStation.getCommissioningDate()));
        }
        //更改结束结束时间
        Long endMonthTime = Math.min(DateTimeUtil.getEndOfMonth(new Date(time)),(offGridEnergyTask.getYesterdayTimestamp() + 3600 *24 * 1000 -1));
        while (startMonthTime < endMonthTime){
            offGridEnergyTask.calculateProductDataDailyByTime(startMonthTime);
            startMonthTime = startMonthTime + 3600 * 24 * 1000;
        }
        offGridEnergyTask.calculateProductDataMonthByTime(endMonthTime);
        return "SUCCESS";
    }

    @ApiOperation(value = "更新某天的生产报表数据")
    @GetMapping(value = "updateProductDateWithDay")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间", dataType = "long")
    })
    public String updateProductDateWithDay(@RequestParam("time")Long time){
        offGridEnergyTask.calculateProductDataDailyByTime(time);
        return "SUCCESS";
    }
}
